// @ts-nocheck
import React, { useEffect, Fragment, useState } from 'react';

import { history } from 'umi';
import { Input, Toast, NavBar } from 'antd-mobile';
import server from '@/utlis/server';

import './index.less';
import '@/pages/golobo.less';
import 'lib-flexible';

export default function index(props) {
  let [user, setuser] = useState({});
  const back = () => {
    history.go(-1);
  };
  const right = (
    <span
      style={{ fontSize: '0.35rem', color: '#333' }}
      onClick={() => {
        ChangeUsername();
      }}
    >
      保存
    </span>
  );
  let ChangeUsername = async () => {
    let { data } = await server.post('/editname', user);
    if (data.msg) {
      return Toast.show({
        icon: 'fail',
        content: data.msg,
      });
    } else {
      // console.log(data);
      localStorage.setItem('token', data.token);
      history.push({
        pathname: '/userinfo',
        state: {
          _id: data.user._id,
        },
      });
    }
  };
  // 获取用户信息
  let getuser = async () => {
    let id = props.location.state._id;
    let { data } = await server.get('/userinfo/' + id);
    // console.log(data);
    setuser(data.data);
  };
  useEffect(() => {
    getuser();
  }, []);
  return (
    <div className="edituser">
      <Fragment>
        <NavBar onBack={back} right={right}>
          {props.location.state.title}
        </NavBar>
        <div className="input">
          <Input
            placeholder="请输入内容"
            clearable
            value={user.username}
            onChange={(value) => {
              user.username = value;
              setuser({ ...user });
              // ChangeUsername(value)
            }}
          />
        </div>
      </Fragment>
    </div>
  );
}
